import {getObject, getQuery} from "../databaseUtil";
import {MyMap} from "../lodash";

export async function dao_save(classname, options) {
    var o = getObject(classname)
    var q = getQuery(classname)
    var one = await q.equalTo('objectId', options.objectId).first().then(res => res.toJSON())
    if (one) {
        //save
        o.id = options.objectId
        MyMap(options, (value, key) => {
            if (key != 'objectId') {
                o.set(key, value)
            }

        })
    } else {
        //add
        MyMap(options, (value, key) => {
            if (key != 'objectId') {
                o.set(key, value)
            }
        })
    }


    return await  o.save().then(res => res.toJSON())
}